package com.ascendere9.ff.pof.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.params.HttpConnectionParams;

import android.net.http.AndroidHttpClient;

/**
 * @author: ThanhNH
 */
public class HttpUtil {

	// ===================================================================
	// Constants
	// ===================================================================
	private static final String LOG_TAG = HttpUtil.class.getSimpleName();

	private static final String USER_AGENT = null;
	private static final int TIME_OUT_CONNECTION = 10000;
	private static final int TIME_OUT_SOCKET = 15000;

	// ===================================================================
	// Fields
	// ===================================================================

	// ===================================================================
	// Constructors
	// ===================================================================

	// ===================================================================
	// Getter & Setter
	// ===================================================================
	public static String getStringResponeByPostMethod(String pUrl, List<NameValuePair> pNameValuePairs,
			final File pBuffer) {
		return HttpUtil.excuteByPostMethod(pUrl, pNameValuePairs, new IHandleHttpResult<String>() {

			@Override
			public String onHandleResult(HttpResponse pHttpResponse) {
				try {
					if (pBuffer == null) {
						InputStream is = pHttpResponse.getEntity().getContent();
						return IOUtil.inputStreamToString(is);
					} else {
						FileOutputStream output = new FileOutputStream(pBuffer);
						pHttpResponse.getEntity().writeTo(output);
						output.close();
						return IOUtil.fileToString(pBuffer);
					}
				} catch (IllegalStateException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				return null;
			}
		});
	}

	// ===================================================================
	// Methods from BaseClass & Interfaces
	// ===================================================================

	// ===================================================================
	// Methods
	// ===================================================================

	private static <T> T excuteByPostMethod(String pUrl, List<NameValuePair> pNameValuePairs,
			IHandleHttpResult<T> pI) {
		T t = null;
		AndroidHttpClient client = getAndroidHttpClient();
		HttpPost httpPost = new HttpPost(pUrl);
		httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
		httpPost.setParams(client.getParams());
		HttpResponse response = null;
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(pNameValuePairs, "UTF-8"));
			response = client.execute(httpPost);
			if (response != null && pI != null) {
				t = pI.onHandleResult(response);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			client.close();
		}
		return t;
	}

	private static AndroidHttpClient getAndroidHttpClient() {
		AndroidHttpClient client = AndroidHttpClient.newInstance(USER_AGENT);
		HttpConnectionParams.setConnectionTimeout(client.getParams(), TIME_OUT_CONNECTION);
		HttpConnectionParams.setSoTimeout(client.getParams(), TIME_OUT_SOCKET);
		return client;
	}

	// ===================================================================
	// Methods for SubClass
	// ===================================================================

	// ===================================================================
	// Inner and Anonymous Classes
	// ===================================================================
	private static interface IHandleHttpResult<T> {
		public T onHandleResult(HttpResponse pHttpResponse);
	}
}
